package com.javapandeng.controller;

import com.javapandeng.base.BaseController;
import com.javapandeng.po.Manage;
import com.javapandeng.po.User;
import com.javapandeng.service.ManageService;
import com.javapandeng.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


/**
 * 账户层
 */
@Controller
@RequestMapping("/manage")
public class ManageController extends BaseController {

    @Autowired
    private ManageService manageService;

    @RequestMapping("/findBySql")
    public String findBySql(Model model, Manage manage){
        String sql = "select * from manage where 1=1";
        if(!isEmpty(manage.getUserName())){
            sql += " and userName like '%"+manage.getUserName()+"%' ";
        }
        sql+=" order by id";
        Pager<Manage> pagers = manageService.findBySqlRerturnEntity(sql);
        model.addAttribute("pagers",pagers);
        model.addAttribute("obj",manage);
        return "manage/manage";
    }

    /**
     * 删除账号
     */
    @RequestMapping("/delete")
    public String delete(Integer id){
        manageService.deleteById(id);
        return "redirect:/manage/findBySql";
    }

    /**
     * 跳转到添加页面
     */

    @RequestMapping("/add")
    public String add(Model model){
        String sql = "select * from manage  order by id";
        List<Manage> listBySqlReturnEntity = manageService.listBySqlReturnEntity(sql);
        model.addAttribute("types",listBySqlReturnEntity);
        return "manage/add";
    }

    /**
     * 添加执行
     */
    @RequestMapping("/exAdd")
    public String exAdd(Manage manage)  {
        manageService.insert(manage);
        return "redirect:/manage/findBySql";
    }

    /**
     * 跳转到修改页面
     */
    @RequestMapping("/update")
    public String update(Integer id,Model model){
        Manage obj = manageService.load(id);
        String sql = "select * from manage  order by id";
        List<Manage> listBySqlReturnEntity = manageService.listBySqlReturnEntity(sql);
        model.addAttribute("types",listBySqlReturnEntity);
        model.addAttribute("obj",obj);
        return "manage/update";
    }



    /**
     * 执行修改信息的操作
     */
    @RequestMapping("/exUpdate")
    public String exUpdate(Manage manage) {
        manageService.updateById(manage);
        return "redirect:/manage/findBySql";
    }


}
